<?php
/**
 * RcUpload class file.
 *
 * @author         RcPHP Dev Team
 * @copyright      Copyright (c) 2013,RcPHP Dev Team
 * @license        Apache License 2.0 {@link http://www.apache.org/licenses/LICENSE-2.0}
 * @package        libraries
 * @since          1.0
 * @filesource
 */
defined('IN_RCPHP') or exit('Access denied');

class RcUpload extends RcBase
{

	/**
	 * 文件大小
	 * @var int
	 */
	protected $limitSize;

	/**
	 * 文件名字
	 * @var string
	 */
	protected $fileName;

	/**
	 * 文件类型
	 * @var string
	 */
	protected $limitType;

	/**
	 * 扩展名
	 * @var string
	 */
	public $ext;

	/**
	 * 构造函数
	 * @return boolean
	 */
	public function __construct()
	{

		$this->limitSize = 2097152; //默认文件大小 2M
		return true;
	}

	/**
	 * 初始化运行环境
	 * @param string $file
	 * @return boolean
	 */
	public function parseInit($file)
	{

		$this->fileName = $file;

		if($this->fileName['size'] > $this->limitSize)
		{
			trigger_error('文件:' . $this->fileName['name'] . ' 大小超出上传限制', E_USER_WARNING);
			exit();
		}
		if($this->limitType)
		{
			$this->parseMimetype($file);
		}

		return true;
	}

	/**
	 * 设置上传文件的大小限制
	 * @param integer $size
	 * @return unkonow
	 */
	public function setLimitSize($size)
	{

		if($size)
		{
			$this->limitSize = $size;
		}

		return $this;
	}

	/**
	 * 设置上传文件允许的格式
	 * @param string $type
	 * @return unkonow
	 */
	public function setLimitType($type)
	{

		if(!$type || !is_array($type))
		{
			return false;
		}

		$this->limitType = $type;

		return $this;
	}

	/**
	 * 验证上传文件的格式
	 * @return boolean
	 */
	protected function parseMimetype($file)
	{
		$mimeType['chm'] = 'application/octet-stream';
		$mimeType['ppt'] = 'application/vnd.ms-powerpoint';
		$mimeType['xls'] = 'application/vnd.ms-excel';
		$mimeType['doc'] = 'application/msword';
		$mimeType['exe'] = 'application/octet-stream';
		$mimeType['rar'] = 'application/octet-stream';
		$mimeType['js'] = "javascrīpt/js";
		$mimeType['css'] = "text/css";
		$mimeType['hqx'] = "application/mac-binhex40";
		$mimeType['bin'] = "application/octet-stream";
		$mimeType['oda'] = "application/oda";
		$mimeType['pdf'] = "application/pdf";
		$mimeType['ai'] = "application/postsrcipt";
		$mimeType['eps'] = "application/postsrcipt";
		$mimeType['es'] = "application/postsrcipt";
		$mimeType['rtf'] = "application/rtf";
		$mimeType['mif'] = "application/x-mif";
		$mimeType['csh'] = "application/x-csh";
		$mimeType['dvi'] = "application/x-dvi";
		$mimeType['hdf'] = "application/x-hdf";
		$mimeType['nc'] = "application/x-netcdf";
		$mimeType['cdf'] = "application/x-netcdf";
		$mimeType['latex'] = "application/x-latex";
		$mimeType['ts'] = "application/x-troll-ts";
		$mimeType['src'] = "application/x-wais-source";
		$mimeType['zip'] = "application/x-zip-compressed";
		$mimeType['bcpio'] = "application/x-bcpio";
		$mimeType['cpio'] = "application/x-cpio";
		$mimeType['gtar'] = "application/x-gtar";
		$mimeType['shar'] = "application/x-shar";
		$mimeType['sv4cpio'] = "application/x-sv4cpio";
		$mimeType['sv4crc'] = "application/x-sv4crc";
		$mimeType['tar'] = "application/x-tar";
		$mimeType['ustar'] = "application/x-ustar";
		$mimeType['man'] = "application/x-troff-man";
		$mimeType['sh'] = "application/x-sh";
		$mimeType['tcl'] = "application/x-tcl";
		$mimeType['tex'] = "application/x-tex";
		$mimeType['texi'] = "application/x-texinfo";
		$mimeType['texinfo'] = "application/x-texinfo";
		$mimeType['t'] = "application/x-troff";
		$mimeType['tr'] = "application/x-troff";
		$mimeType['roff'] = "application/x-troff";
		$mimeType['shar'] = "application/x-shar";
		$mimeType['me'] = "application/x-troll-me";
		$mimeType['ts'] = "application/x-troll-ts";
		$mimeType['gif'] = "image/gif";
		$mimeType['jpeg'] = "image/pjpeg";
		$mimeType['jpg'] = "image/pjpeg";
		$mimeType['jpe'] = "image/pjpeg";
		$mimeType['ras'] = "image/x-cmu-raster";
		$mimeType['pbm'] = "image/x-portable-bitmap";
		$mimeType['ppm'] = "image/x-portable-pixmap";
		$mimeType['xbm'] = "image/x-xbitmap";
		$mimeType['xwd'] = "image/x-xwindowdump";
		$mimeType['ief'] = "image/ief";
		$mimeType['tif'] = "image/tiff";
		$mimeType['tiff'] = "image/tiff";
		$mimeType['pnm'] = "image/x-portable-anymap";
		$mimeType['pgm'] = "image/x-portable-graymap";
		$mimeType['png'] = "image/png";
		$mimeType['rgb'] = "image/x-rgb";
		$mimeType['xpm'] = "image/x-xpixmap";
		$mimeType['txt'] = "text/plain";
		$mimeType['c'] = "text/plain";
		$mimeType['cc'] = "text/plain";
		$mimeType['h'] = "text/plain";
		$mimeType['html'] = "text/html";
		$mimeType['htm'] = "text/html";
		$mimeType['htl'] = "text/html";
		$mimeType['rtx'] = "text/richtext";
		$mimeType['etx'] = "text/x-setext";
		$mimeType['tsv'] = "text/tab-separated-values";
		$mimeType['mpeg'] = "video/mpeg";
		$mimeType['mpg'] = "video/mpeg";
		$mimeType['mpe'] = "video/mpeg";
		$mimeType['avi'] = "video/x-msvideo";
		$mimeType['qt'] = "video/quicktime";
		$mimeType['mov'] = "video/quicktime";
		$mimeType['moov'] = "video/quicktime";
		$mimeType['movie'] = "video/x-sgi-movie";
		$mimeType['au'] = "audio/basic";
		$mimeType['snd'] = "audio/basic";
		$mimeType['wav'] = "audio/x-wav";
		$mimeType['aif'] = "audio/x-aiff";
		$mimeType['aiff'] = "audio/x-aiff";
		$mimeType['aifc'] = "audio/x-aiff";
		$mimeType['swf'] = "application/x-shockwave-flash";

		$mimeTypeKey = array_keys($mimeType);

		foreach($this->limitType as $type)
		{
			if(!in_array($type, $mimeTypeKey))
			{
				return false;
			}
		}

		$allowTypeArray = array();
		foreach($this->limitType as $type)
		{
			$allowTypeArray[] = $mimeType[$type];
		}

		if(!in_array($this->fileName['type'], $allowTypeArray))
		{
			return false;
		}
		$this->ext = array_search($this->fileName['type'], $mimeType);

		return true;
	}

	/**
	 * 上传文件
	 * @param string $fileUpload
	 * @param string $fileName
	 * @return boolean
	 */
	public function upload($fileUpload, $fileName)
	{
		if(!is_array($fileUpload) || empty($fileName))
		{
			return false;
		}

		$this->parseInit($fileUpload);

		if(!move_uploaded_file($this->fileName['tmp_name'], $fileName . '.' . $this->ext))
		{
			RcDebug::addMessage("<font color='red'>文件" . $fileName . "上传失败</font>");

			return false;
		}

		return true;
	}
}